package com.quktzhuanka.app.my;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.loopj.android.http.RequestParams;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;
import com.quktzhuanka.app.CaiNiaoApplication;
import com.quktzhuanka.app.R;
import com.quktzhuanka.app.adapter.InviteAdapter;
import com.quktzhuanka.app.base.BaseActivity;
import com.quktzhuanka.app.bean.BannerBean;
import com.quktzhuanka.app.bean.Response;
import com.quktzhuanka.app.common.SPUtils;
import com.quktzhuanka.app.common.T;
import com.quktzhuanka.app.config.Constants;
import com.quktzhuanka.app.https.HttpUtils;
import com.quktzhuanka.app.https.onOKJsonHttpResponseHandler;
import com.quktzhuanka.app.utils.ImgUtils;
import com.quktzhuanka.app.utils.WxUtil;
import com.quktzhuanka.app.wmm.QQShareUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jiguang.analytics.android.api.CountEvent;
import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import cz.msebera.android.httpclient.Header;

/**
 * 分享推荐
 * Created by yohn on 2018/7/14.
 */

public class MyShareUrlActivity extends BaseActivity implements IUiListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_left)
    TextView tv_left;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private InviteAdapter adapter;
    private List<BannerBean> list=new ArrayList<>();
    private static final float MIN_SCALE = .95f;
    private static final float MAX_SCALE = 1.00f;
    private LinearLayoutManager mLinearLayoutManager;
    private int mMinWidth;
    private int mScreenWidth;
    private Bitmap bitmap;
    private String url;
    @BindView(R.id.view_zz)
    View zz;
    @BindView(R.id.ll_share)
    LinearLayout llShare;
    @Override
    protected void initUI() {
        setContentView(R.layout.ac_invite);
        ButterKnife.bind(this);
        tvTitle.setText( "邀请好友" );
        tv_left.setVisibility( View.VISIBLE );
        getBanner();
    }

    @Override
    protected void initData() {
        if("Y".equals(CaiNiaoApplication.getUserInfoBean().user_msg.is_share_vip)){
            url=SPUtils.getStringData(getComeActivity(),"share_url_vip","")+"/wap.php/UserAccount/register/referrer_id/"+ SPUtils.getStringData(this,Constants.UID,"");
        }else{
//             url=Constants.SHARE_URL_REGISTER;
            url=Constants.SHARE_URL_REGISTER+"/referrer_id/"+ SPUtils.getStringData(this,Constants.UID,"");
        }
        mLinearLayoutManager = new LinearLayoutManager(this);
        mLinearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        mMinWidth = (int) (mScreenWidth * 0.28f);
        LinearLayoutManager ms= new LinearLayoutManager(this);
        ms.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(ms);
        adapter=new InviteAdapter(this, url,list);
        recyclerView.setAdapter(adapter);
        if(Build.VERSION.SDK_INT>=23)
            recyclerView.setOnScrollListener(mOnScrollListener);
    }
    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            final int childCount = recyclerView.getChildCount();
            for (int i = 0; i < childCount; i++) {
                LinearLayout child = (LinearLayout) recyclerView.getChildAt(i);
                RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
                int left = child.getLeft();
                int right = mScreenWidth - child.getRight();
                final float percent = left < 0 || right < 0 ? 0 : Math.min(left, right) * 1f / Math.max(left, right);
                float scaleFactor = MIN_SCALE + Math.abs(percent) * (MAX_SCALE - MIN_SCALE);
                child.setLayoutParams(lp);
                child.setScaleY(scaleFactor);
            }
        }
    };
    @Override
    protected void initListener() {

    }
    private void getBanner(){
        RequestParams requestParams = new RequestParams();
        requestParams.put("cat_id", 4);
        HttpUtils.post(Constants.GET_BANNER, requestParams, new onOKJsonHttpResponseHandler<BannerBean>(new TypeToken<Response<BannerBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(throwable.getMessage());
            }

            @Override
            public void onSuccess(int statusCode, Response<BannerBean> datas) {
                if (datas.isSuccess()) {
                    list.clear();
                    list.addAll(datas.getData().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }

    @OnClick({R.id.txt_finish,R.id.btn_copy,R.id.btn_invite,R.id.tv_left,R.id.copy_friends_qq,R.id.copy_friends_cicle_zone, R.id.view_zz, R.id.copy_friends_cicle_btn, R.id.copy_friends_btn})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.view_zz:
                zz.setVisibility(View.GONE);
                llShare.setVisibility(View.GONE);
                break;
            case R.id.txt_finish:
                zz.setVisibility(View.GONE);
                llShare.setVisibility(View.GONE);
                break;
            case R.id.btn_copy:
                ClipboardManager myClipboard;
                myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
                ClipData myClip;
                myClip = ClipData.newPlainText("text", url);
                myClipboard.setPrimaryClip(myClip);
                ToastUtils.showShortToast(MyShareUrlActivity.this,"复制成功");
                break;
            case R.id.copy_friends_qq:
                if (getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq") == null) {
                    T.showShort(this, "请安装QQ客户端");
                    return;
                }
                String str=ImgUtils.saveImageToGallery2(this,bitmap);
                if("".equals(str)){
                    T.showShort(this, "分享失败");
                    return;
                }
                QQShareUtil.shareImgToQQ(str, this, this);
                addTime();
                zz.setVisibility(View.GONE);
                llShare.setVisibility(View.GONE);
                break;
            case R.id.copy_friends_cicle_zone:
                if (getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq") == null) {
                    T.showShort(this, "请安装QQ客户端");
                    return;
                }
                String str1=ImgUtils.saveImageToGallery2(this,bitmap);
                if("".equals(str1)){
                    T.showShort(this, "分享失败");
                    return;
                }
                QQShareUtil.shareImgToQQZONE(str1, this,this);
                addTime();
                zz.setVisibility(View.GONE);
                llShare.setVisibility(View.GONE);
                break;
            case R.id.copy_friends_cicle_btn:
                if (getPackageManager().getLaunchIntentForPackage("com.tencent.mm") == null) {
                    T.showShort(this, "请安装微信客户端");
                    return;
                }
                WxUtil.sharePicByBitMap(bitmap, "pyq", SendMessageToWX.Req.WXSceneTimeline, MyShareUrlActivity.this);
                addTime();
                zz.setVisibility(View.GONE);
                llShare.setVisibility(View.GONE);
                break;
            case R.id.copy_friends_btn:
                if (getPackageManager().getLaunchIntentForPackage("com.tencent.mm") == null) {
                    T.showShort(this, "请安装微信客户端");
                    return;
                }
                WxUtil.sharePicByBitMap(bitmap, "pyq", SendMessageToWX.Req.WXSceneSession, MyShareUrlActivity.this);
                addTime();
                zz.setVisibility(View.GONE);
                llShare.setVisibility(View.GONE);
                break;
            case R.id.btn_invite:
                bitmap=adapter.getBitmap();
                if(bitmap==null){
                    showToast("请选择分享的海报");
                    return;
                }
                zz.setVisibility(View.VISIBLE);
                llShare.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_left:
                finish();
                break;
        }
    }
    private void  addTime(){
        JAnalyticsInterface.onEvent(this,new CountEvent("copy_invite"));
    }
    @Override
    public void onComplete(Object o) {

    }

    @Override
    public void onError(UiError uiError) {

    }

    @Override
    public void onCancel() {

    }
}
